package com.shangyueshang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shangyueshang.common.SysConstants;
import com.shangyueshang.mapper.FreightForwarderMapper;
import com.shangyueshang.model.FreightForwarder;
import com.shangyueshang.request.FreightDTO;
import com.shangyueshang.request.PageReq;
import com.shangyueshang.response.HttpResult;
import com.shangyueshang.response.PageResult;
import com.shangyueshang.service.FreightForwarderService;
import com.shangyueshang.utils.MyPageHelper;
import com.shangyueshang.utils.SecurityUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * @author: JokeyZheng
 * @email: zhengjingfeng@ruqimobility.com
 * @created: 2022/2/18 17:57
 * @version: v1.0.0
 */
@Service
public class FreightForwarderServiceImpl
        extends ServiceImpl<FreightForwarderMapper, FreightForwarder> implements FreightForwarderService {

    @Override
    public PageResult<FreightForwarder> findPage(PageReq pageReq) {
        QueryWrapper<FreightForwarder> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc(SysConstants.create_time);
        pageReq.getLineParams().forEach((k, v) -> {
            if (k.equals(SysConstants.name)) {
                queryWrapper.like(k, v);
            } else if (k.equals(SysConstants.advantages)) {
                queryWrapper.like(k, v);
            } else {
                queryWrapper.eq(k, v);
            }
        });
        PageInfo<FreightForwarder> pageInfo =
                PageHelper.startPage(
                        pageReq.getPageNum(),
                        pageReq.getPageSize()
                ).doSelectPageInfo(() -> super.list(queryWrapper));
        return MyPageHelper.getPageResult(pageInfo);
    }

    @Override
    public HttpResult<Boolean> saveUpdate(FreightDTO request) {
        FreightForwarder _freight = super.getOne(new QueryWrapper<FreightForwarder>().eq(SysConstants.name, request.getName()));
        if (_freight == null) {
            _freight = new FreightForwarder();
            _freight.setCreator(SecurityUtil.getUsername());
        }
        _freight.setLastUpdator(SecurityUtil.getUsername());
        BeanUtils.copyProperties(request, _freight);
        return HttpResult.ok(super.saveOrUpdate(_freight));
    }
}
